package com.dreamstations.changshads.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.GravityCompat;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Toast;

import com.dreamstations.changshads.R;
import com.dreamstations.changshads.adapter.HomeNavigationAdapter;
import com.dreamstations.changshads.adapter.Home_FragmentAdapter;
import com.dreamstations.changshads.fragment.Datum_Header_Frag;
import com.dreamstations.changshads.fragment.Datum_Simple_Fragment;
import com.dreamstations.changshads.fragment.Home_Frag;
import com.dreamstations.changshads.fragment.Investigate_Event_Frag;
import com.dreamstations.changshads.fragment.Investigate_Fact_Frag;
import com.dreamstations.changshads.fragment.Topic_Header_Frag;
import com.dreamstations.changshads.fragment.Topic_Simple_Frag;
import com.dreamstations.changshads.fragment.Work_Frontier_Frag;
import com.dreamstations.changshads.fragment.Work_Introduction_Frag;
import com.dreamstations.changshads.fragment.Work_Simple_Frag;
import com.dreamstations.changshads.util.L;
import com.dreamstations.changshads.util.RequestType;
import com.dreamstations.changshads.view.BottomBar;

import java.util.ArrayList;
import java.util.List;

public class HomeActivity extends AppCompatActivity {
    private static final String TAG = HomeActivity.class.getName();
    Toolbar toolbar;
    DrawerLayout drawer;
    LinearLayout home_content;
    LinearLayout toolbar_content_home;
    TabLayout toolbar_content_tablayout;
    ViewPager viewPager;
    Home_FragmentAdapter homeAdapter, workAdapter, investigateAdapter, topicAdapter, datumAdapter;
    BottomBar bottomBar;
    ImageView imgheader;
    FragmentManager fragmentManager;
    private static int headerClickCount = 0;

    BottomBar.OnBottomItemChangeListener onBottomItemChangeListener = new BottomBar.OnBottomItemChangeListener() {
        @Override
        public void onSelectedItemChanged(int selected) {
            if (selected == 0) {
                toolbar_content_home.setVisibility(View.VISIBLE);
                toolbar_content_tablayout.setVisibility(View.GONE);
            } else {
                toolbar_content_home.setVisibility(View.GONE);
                toolbar_content_tablayout.setVisibility(View.VISIBLE);
            }
            viewPager.removeAllViews();
            switch (selected) {
                case 0:
                    loadHome();
                    break;
                case 1:
                    loadWork();
                    break;
                case 2:
                    loadInvestigate();
                    break;
                case 3:
                    loadTopic();
                    break;
                case 4:
                    loadDatum();
                    break;
                default:
                    break;
            }
        }
    };

    private void loadHome() {

        L.i(TAG, "->loadHome");
        if (homeAdapter == null) {
            List<Fragment> fragments = new ArrayList<>();
            fragments.add(new Home_Frag());
            homeAdapter = new Home_FragmentAdapter(fragmentManager, fragments);
        }
        viewPager.setAdapter(homeAdapter);
        homeAdapter.notifyDataSetChanged();
        toolbar_content_tablayout.setupWithViewPager(viewPager);
    }

    private void loadWork() {
        if (workAdapter == null) {
            List<Fragment> fragments = new ArrayList<>();
            fragments.add(new Work_Introduction_Frag("党史工作", RequestType.TYPE_WORK, 1));
            fragments.add(new Work_Frontier_Frag("党史机构", RequestType.TYPE_WORK, 2));
            fragments.add(new Work_Simple_Frag("区县之窗", RequestType.TYPE_WORK, 3));
            fragments.add(new Work_Simple_Frag("党史学会", RequestType.TYPE_WORK, 4));
            fragments.add(new Work_Simple_Frag("专家智库", RequestType.TYPE_WORK, 5));
            fragments.add(new Work_Simple_Frag("党史宣讲团", RequestType.TYPE_WORK, 6));
            fragments.add(new Work_Simple_Frag("公示公开", RequestType.TYPE_WORK, 7));
            workAdapter = new Home_FragmentAdapter(fragmentManager, fragments);
        }
        viewPager.setAdapter(workAdapter);
        workAdapter.notifyDataSetChanged();
        toolbar_content_tablayout.setupWithViewPager(viewPager);
    }

    private void loadInvestigate() {
        if (investigateAdapter == null) {
            List<Fragment> fragments = new ArrayList<>();
            fragments.add(new Investigate_Event_Frag("时政新闻", RequestType.TYPE_INVESTIGATE, 1));
            fragments.add(new Investigate_Event_Frag("学术前沿", RequestType.TYPE_INVESTIGATE, 2));
            fragments.add(new Investigate_Event_Frag("红色文化", RequestType.TYPE_INVESTIGATE, 3));
            fragments.add(new Investigate_Event_Frag("论点纵横", RequestType.TYPE_INVESTIGATE, 4));
            fragments.add(new Investigate_Event_Frag("大事记述", RequestType.TYPE_INVESTIGATE, 5));
            fragments.add(new Investigate_Fact_Frag("史实考证", RequestType.TYPE_INVESTIGATE, 6));
            investigateAdapter = new Home_FragmentAdapter(fragmentManager, fragments);
        }
        viewPager.setAdapter(investigateAdapter);
        investigateAdapter.notifyDataSetChanged();
        toolbar_content_tablayout.setupWithViewPager(viewPager);
    }

    private void loadTopic() {
        if (topicAdapter == null) {
            List<Fragment> fragments = new ArrayList<>();
            fragments.add(new Topic_Header_Frag("特别策划", RequestType.TYPE_TOPIC, 1));
            fragments.add(new Topic_Simple_Frag("学习专题", RequestType.TYPE_TOPIC, 2));
            fragments.add(new Topic_Simple_Frag("热点话题", RequestType.TYPE_TOPIC, 3));
            fragments.add(new Topic_Simple_Frag("人物专访", RequestType.TYPE_TOPIC, 4));
            fragments.add(new Topic_Simple_Frag("图片专题", RequestType.TYPE_TOPIC, 5));
            topicAdapter = new Home_FragmentAdapter(fragmentManager, fragments);
        }
        viewPager.setAdapter(topicAdapter);
        topicAdapter.notifyDataSetChanged();
        toolbar_content_tablayout.setupWithViewPager(viewPager);
    }

    private void loadDatum() {
        if (datumAdapter == null) {
            List<Fragment> fragments = new ArrayList<>();
//            fragments.add(new Datum_Header_Frag("党史课堂", RequestType.TYPE_DATUM_LITERATURE, 1));
            fragments.add(new Datum_Simple_Fragment("党史课堂", RequestType.TYPE_DATUM_LITERATURE, 1));
            fragments.add(new Datum_Simple_Fragment("纪念活动", RequestType.TYPE_DATUM_LITERATURE, 2));
            fragments.add(new Datum_Simple_Fragment("党史文艺", RequestType.TYPE_DATUM_LITERATURE, 3));
//            fragments.add(new Datum_Simple_Fragment("经典影视", RequestType.TYPE_DATUM,3));
//            fragments.add(new Datum_Simple_Fragment("经典歌曲", RequestType.TYPE_DATUM,4));
//            fragments.add(Datum_Reading_Frag.newInstance());
            datumAdapter = new Home_FragmentAdapter(fragmentManager, fragments);
        }
        viewPager.setAdapter(datumAdapter);
        datumAdapter.notifyDataSetChanged();
        toolbar_content_tablayout.setupWithViewPager(viewPager);
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.home_activity);
        fragmentManager = getSupportFragmentManager();

        initViews();
        setNavigationList();
        imgheader.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (headerClickCount == 4) {
                    startActivity(new Intent(HomeActivity.this, FactoryActivity.class));
                    headerClickCount = 0;

                } else if (headerClickCount == 2) {
                    Toast.makeText(getApplicationContext(),
                            "再点击2下进入工厂模式", Toast.LENGTH_SHORT).show();
                    headerClickCount++;
                } else {
                    headerClickCount++;
                }
            }
        });
    }

    private void setNavigationList() {
        ListView navList = (ListView) findViewById(R.id.navigation_listview);
        BaseAdapter navAdatper = new HomeNavigationAdapter(getApplicationContext());
        navList.setAdapter(navAdatper);
        navList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//                Toast.makeText(getApplicationContext(),"=>NavList onItemClick:"+id,Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(getBaseContext(), SectionActivity.class);
                intent.putExtra("sectionType", new Long(id).intValue());
                startActivity(intent);
//                DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
//                drawer.closeDrawer(GravityCompat.START);
            }
        });
    }

    private void initViews() {
        bottomBar = (BottomBar) findViewById(R.id.bottombar);
        home_content = (LinearLayout) findViewById(R.id.home_content);
        drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        toolbar_content_home = (LinearLayout) findViewById(R.id.toolbar_content_home);
        toolbar_content_tablayout = (TabLayout) findViewById(R.id.toolbar_content_tablayout);
        viewPager = (ViewPager) findViewById(R.id.home_viewpager);
        viewPager.setOffscreenPageLimit(0);
        bottomBar.setOnBottomItemChangeListener(onBottomItemChangeListener);
        imgheader = (ImageView) findViewById(R.id.drawer_imgheader);
        toolbar.setTitle("");
        setSupportActionBar(toolbar);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.addDrawerListener(toggle);
        drawer.addDrawerListener(drawerListener);
        toggle.syncState();


        loadHome();
    }


    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }

    }

    /**
     * 使得NavigationLayout出来的时候，主页面也会随之滑出
     */
    DrawerLayout.DrawerListener drawerListener = new DrawerLayout.DrawerListener() {
        @Override
        public void onDrawerSlide(View drawerView, float slideOffset) {
            float offset = slideOffset * drawerView.getMeasuredWidth();
            home_content.setTranslationX(offset);
        }

        @Override
        public void onDrawerOpened(View drawerView) {

        }

        @Override
        public void onDrawerClosed(View drawerView) {

        }

        @Override
        public void onDrawerStateChanged(int newState) {

        }
    };

}
